<?php
/*
课件管理模块
*/
include_once('include/top.php');

$action=fs_get_value("do");
switch($action)
{
	case	"addcourseware"	     : addcourseware()	             ; break;
	case	"add"	             : do_addcourseware()	     ; break;
	case	"delete"	     : delete_courseware()	     ; break;
	case    "searchcourseware"   : searchcourseware()            ; break;
	default                      : main()                        ; break;
}
?>

<script type="text/javascript">
function Check(){
    if(document.addform.title.value ==  ""){
        alert("课件标题不能为空!");
        document.addform.title.focus();
        return  false;
    }
}
</script>
<?php

function main()
{
    global $db,$teacher_id,$teacher_passport;
	$title = fs_get_value("title");
//	$writer = fs_get_value("writer");
//	$writerid = fs_get_value("writerid");
        $state = fs_get_value("state");
	$times = fs_get_value("times");
	$xuhao = fs_get_value("xuhao");
	$intotime = fs_get_value("intotime");
	$introduction = fs_get_value("introduction1");
	$conditionarray = array($title,$state,$times,$xuhao,$intotime,$introduction);
	$valuearray = array(" and title like '%".$title."%'"," and state = '".$state."'"," and times = ".$times," and xuhao = ".$xuhao," and intotime = ".strtotime($intotime)," and introduction like '%".$introduction."%'");
	$sql = "select * from nettteach_courseware where writerid = '".$teacher_id."'";
	for($i=0;$i<count($conditionarray);$i++ ){
		if($conditionarray[$i] == ''){
			$valuearray[$i] = '';
		}
		$sql.=$valuearray[$i];
	}
	$courseware_result = $db -> query($sql);
	$recordcount = $db -> num_rows($courseware_result);
	@$page=$_GET['p'];
	if ($page==null){$page=1;}
	$pagesize=15;								//每页显示数
	$pagecount=ceil($recordcount/$pagesize);	//计算总页
	if ($page>$pagecount){$page=$pagecount;}
	if ($page<1){$page=1;}
?>
<script src="./js/Main.js"></script>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="CContent">
  <tr>
    <th class="CTitle">课件管理</th>
  </tr>
  <tr>
    <td class="CPanel">
    <div class="SubMenu">
		<a href="teacher_courseware.php?">课件管理</a> | 
		<a href="teacher_courseware.php?do=addcourseware">添加课件</a> | 
		<a href="teacher_courseware.php?do=searchcourseware">搜索课件</a>
	</div>
	
	<form action="?do=delete" method="post" name="fooeeform" style="margin:0px">
		<table border="0" cellpadding="0" cellspacing="0"  class="TablePanel" width="100%">
			<tr align="center" class="TDHead">
				<td nowrap><input type=button style="font-size:9pt;width:50px;" value="全选" onclick="click_allid(fooeeform,1)"/></td>
				<td nowrap><input type=button style="font-size:9pt;width:60px;" value="全取消" onclick="click_allid(fooeeform,0)"/></td>
				<td nowrap><input type="submit" style="font-size:9pt;width:50px;" value="删除" onclick="return delid()"/></td>

				<td nowrap width="500"><input type=button style="font-size:9pt;width:80px;" onclick="window.location.href('teacher_courseware.php?do=searchcourseware')" value="搜索课件" />
				</td>
				<td nowrap>.</td>
				<td nowrap>.</td>
				<td width="30%" nowrap></td>
				<td nowrap></td>
			</tr>
		</table>
<div id="click_idall">
    <div align="left" style="padding:5px;">
      <table border="0" cellpadding="0" cellspacing="0" class="TablePanel" align="center" width="100%">
        <tr align="center">
		  <td class="TDHead">√</td>
          <td class="TDHead" nowrap>课件标题</td>
<!--          <td class="TDHead" nowrap>所属教师</td>
                  <td class="TDHead" nowrap>所属系/专业</td>
          <td class="TDHead" nowrap>登陆账号</td>-->
          <td class="TDHead" nowrap>课件状态</td>
          <td class="TDHead" nowrap>下载次数</td>
          <td class="TDHead" nowrap>大小</td>
<!--          <td class="TDHead" nowrap>排列序号</td>-->
		  <td class="TDHead" nowrap>入库时间</td>
          <td class="TDHead" nowrap>操 作</td>
        </tr>
<?php
	$courseware_result=$db->query($sql." limit ".($page-1)*($pagesize).",$pagesize");
	while($courseware_row = $db->fetch_array($courseware_result))
	{
?>
		<tr id="t<?php echo($courseware_row["id"]);?>" onclick="click_tr('<?php echo ($courseware_row["id"]);?>')" onMouseOver="bgColor='#FF8899'" onMouseOut="bgColor='FFFFFF'" title=单击选中 align="center">
			<td height="27"><input name='id[]' type='checkbox' id="c<?php echo($courseware_row["id"]);?>" value='<?php echo ($courseware_row["id"]);?>' onclick="click_tr('<?php echo ($courseware_row["id"]);?>')"></td>
			<td><a href="?do=addcourseware&courseware_id=<?php echo ($courseware_row["id"]);?>"><?php echo($courseware_row["title"]);?></a></td>
<!--			<td><?php echo(get_from_info('nettteach_member','realname',$courseware_row['writerid']));?></td>
			<td><?php echo(fs_get_depart_major('nettteach_member',$courseware_row['writerid']));?></td>
            <td><?php echo(get_from_info('nettteach_member','passport',$courseware_row['writerid']));?></td>-->
            <td><?php echo(fs_get_state_view($courseware_row['state']));?></td>
            <td><?php echo($courseware_row['times']);?></td>
            <td><?php echo($courseware_row['size']);?>兆</td>
<!--			<td><?php echo($courseware_row['xuhao']);?></td>-->
			<td><?php echo(date("Y-m-d H:i:s",$courseware_row['intotime']));?></td>
			<td><a href="?do=addcourseware&courseware_id=<?php echo ($courseware_row["id"]);?>">编缉</a></td>
		</tr>
		<tr>
			<td height=1 colspan=10 bgcolor="#E8F1FF" onMouseOver="bgColor='#FF8899'" onMouseOut="bgColor='E7sxE7sxE7sx'"></td>
		</tr>
<?php
	}
?>
	</table>
	
	
	
</div>
	</div>
	<center>
<?php
/*分页示例:

pager(传入URL , 数据总数 , 开始条数 , 每页数目)
*/
echo(pager('?p={page}',$recordcount,($page-1)*($pagesize),$pagesize));		
?>
</center>
	</td>
	</tr>
	</table>
</form>
<?php
}
?>


<!--
编缉课件开始
-->
<?php
function addcourseware()
{
    global $db;
	$courseware_id = fs_get_value("courseware_id");
	if($courseware_id != ''){
		$sql = "select * from nettteach_courseware where id = ".$courseware_id;
		$courseware_result = $db -> query($sql);
		$courseware_row = $db -> fetch_array($courseware_result);
	}
?>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="CContent">
		<tr>
		    <th class="CTitle">课件管理</th>
		</tr>	
		<tr>
			<td class="CPanel">
    <div class="SubMenu">
		<a href="teacher_courseware.php?">课件管理</a> | 
		<a href="teacher_courseware.php?do=addcourseware">添加课件</a> | 
		<a href="teacher_courseware.php?do=searchcourseware">搜索课件</a>
	</div>
			</td>
		</tr>	
		<tr>
			<td>
			<div class="middle_right_list">
			<form name="addform" method="post" action="?do=add&courseware_id=<?php echo($courseware_id);?>">
                <fieldset>
                    <legend>添加/编缉课件</legend>
                        带<span style="color:#FF0000;">*</span>的必填。<br/><br>
                        <input type="hidden" name="lastaddress" value="<?php if($courseware_id == ''){echo("/uploadfiles/document/");}else{echo($courseware_row['address']);}?>"  style="width:300px;" />
                        <label><div align="right">课件标题：</div></label><input name="title" type="text" style="width:300px;" class="text" value="<?php echo($courseware_row['title']);?>" /><span style="color:#FF0000;">*</span><br/><br/>
                        <label><div align="right">课件地址：</div></label><input type="hidden" name="address" value="<?php if($courseware_id == ''){echo("/uploadfiles/document/");}else{echo($courseware_row['address']);}?>" style="width:300px;" />
                            <iframe frameborder="0" name="upload_frame_video" scrolling="no" src="teacher_upload_document.php" width="300px" style="min-height:30px;" onload="this.height=this.contentWindow.document.body.scrollHeight"></iframe><span style="color:#FF0000;">*</span><span>只能上传小于200兆的文件，格式不限。</span><br/></br>
                        <label><div align="right">课件大小：</div></label><input name="size" type="text" style="width:300px;" class="text" value="<?php echo($courseware_row['size']);?>" readonly ="true"/><span style="color:#FF0000;">&nbsp;&nbsp;兆</span><br/></br>
                        <label><div align="right">课件状态：</div></label>
                            <select name="state" style="width:300px;">
                                <option value="" <?php if($courseware_row['state'] == null){echo("selected");}?>>请选择状态</option>
                                <option value="0" <?php if($courseware_row['state'] == 0){echo("selected");}?>>显示</option>
                                <option value="1" <?php if($courseware_row['state'] == 1){echo("selected");}?>>不显示</option>
                            </select><span style="color:#FF0000;">*</span><br/></br>
                        <label><div align="right">下载次数：</div></label><input name="times" type="text" style="width:300px;" class="text" value="<?php echo($courseware_row['times']);?>"><br/></br>
<!--                        <label><div align="right">排列序号：</div></label><input name="xuhao" type="text" style="width:300px;" class="text" value="<?php echo($courseware_row['xuhao']);?>"><br/><br/>-->
    <!--					<label><div align="right">入库时间：</div></label><input name="intotime" type="text" style="width:400px;" class="text" value="<?php if($courseware_id == ''){echo(date("Y-m-d H:i:s",time()));}else{echo(date("Y-m-d H:i:s",$courseware_row['intotime']));}?>"><br/><br/>-->
                        <label><div aling="right">课件简介：</div></label><textarea name="introduction1" style="width:500px;height:200px;"><?php echo($courseware_row['introduction']);?></textarea><br /><br/>
                        <input name="intotime" type="hidden" style="width:400px;" class="text" value="<?php if($courseware_id == ''){echo(date("Y-m-d H:i:s",time()));}else{echo(date("Y-m-d H:i:s",$courseware_row['intotime']));}?>">
                         <div class="submit">
                        <label></label><input type="submit" value="确认提交" onclick="return Check();"/>&nbsp;&nbsp;&nbsp;&nbsp;<a class="lj" href="teacher_courseware.php">返回列表</a></div>
                </fieldset>
			</form>
			</div>
			</td>
		</tr>
	</table>
<?php
}
function do_addcourseware(){
    global $db,$teacher_id,$teacher_realname;
	$title = fs_get_value("title");
	$writerid = fs_get_value("writerid");
	$xuhao = fs_get_value("xuhao");
	$intotime = fs_get_value("intotime");
        $size = fs_get_value("size");
        $address = fs_get_value("address");
        $lastaddress = fs_get_value("lastaddress"); //原地址 若换了则删除原文件
	$state = fs_get_value("state");
	$times = fs_get_value("times");
	$introduction = fs_get_value("introduction1");
	//增加课件
	if(fs_get_value("courseware_id") == ''){
		if($title != ''){
			$sql = "insert into nettteach_courseware (title,writerid,writer) value ('".$title."','".$teacher_id."','".$teacher_realname."')";
			$db -> query($sql);
			$id = mysql_insert_id();
		}
	}
	else{
		//修改课件
		$id = fs_get_value("courseware_id");
		if($title != ''){
			$sql = "update nettteach_courseware set title = '".$title."' where id = ".$id;
			$db -> query($sql);
		}
	}
//	if($writerid != ''){
//		$sql = "update nettteach_courseware set writerid = '".$writerid."',writer = '".get_writer($writerid)."' where id = ".$id;
//		$db -> query($sql);
//	}
	if($xuhao != ''){
		$sql = "update nettteach_courseware set xuhao = '".$xuhao."' where id = ".$id;
		$db -> query($sql);
	}
	if($introduction != ''){
		$sql = "update nettteach_courseware set introduction = '".$introduction."' where id = ".$id;
		$db -> query($sql);
	}
	if($intotime == ''){
		$sql = "update nettteach_courseware set intotime = 0 where id = ".$id;
		$db -> query($sql);
	}
	else{
		$sql = "update nettteach_courseware set intotime = ".strtotime($intotime)." where id = ".$id;
		$db -> query($sql);
	}
    if($address != $lastaddress){
		$sql = "update nettteach_courseware set address = '".$address."' where id = ".$id;
		$db -> query($sql);
        $file=$lastaddress;$file = substr($file,1,strlen($lastaddress)); 
        if (unlink($file)) {
//           echo "The file was deleted successfully.", "n";
        } else {
//           echo "The specified file could not be deleted. Please try again.", "n";
        }
	}
        if($size != ''){
		$sql = "update nettteach_courseware set size = '".$size."' where id = ".$id;
		$db -> query($sql);
	}
	if($state != ''){
		$sql = "update nettteach_courseware set state = ".$state." where id = ".$id;
		$db -> query($sql);
	}
	else{
		$sql = "update nettteach_courseware set state = null where id = ".$id;
		$db -> query($sql);
	}
	if($times != ''){
		$sql = "update nettteach_courseware set times = ".$times." where id = ".$id;
		$db -> query($sql);
	}
	if(fs_get_value("courseware_id") == ''){
		fs_showmsg("添加成功！");
	}
	else{
		fs_showmsg("修改成功！");
	}
}
//跟据作者ID获取作者姓名
function get_writer($id){
	global $db;
	$sql = "select * from nettteach_expert where id = ".$id;
	$result = $db -> query($sql);
	$row = $db -> fetch_array($result);
	return $row['name'];
}
//可以查询多个表信息的函数
function get_from_info($table,$str,$id){
	global $db;
	$sql = "select * from ".$table." where id = ".$id;
	$result = $db -> query($sql);
	$row = $db -> fetch_array($result);
	return $row[$str];
}
//根据XX表 的 id 返回系名和专业名 qzr
function fs_get_depart_major($table,$i)
{
	if ( $i=='' || is_null($i) || !is_numeric($i))	{return '无';break;}
	global $db;
	$sql	=	"select d.name as departmentname,m.name as majorname from ".$table." c,nettteach_department d,nettteach_major m where c.id=$i and c.majorid = m.id and c.departmentid = d.id ";
        $result =	$db -> query($sql);
	$row	=	$db -> fetch_array($result);
	return	$row['departmentname']."/".$row['majorname'];
}
?>

<!--
编缉课件结束
-->

<?php
function searchcourseware()
{
?>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="CContent">
		<tr>
		    <th class="CTitle">课件管理</th>
		</tr>	
		<tr>
			<td class="CPanel">
    <div class="SubMenu">
		<a href="teacher_courseware.php?">课件管理</a> | 
		<a href="teacher_courseware.php?do=addcourseware">添加课件</a> | 
		<a href="teacher_courseware.php?do=searchcourseware">搜索课件</a>
	</div>
			</td>
		</tr>	
		<tr>
			<td>
			<div class="middle_right_list">
			<form name="searchform" method="post" action="?">
			<fieldset>
				<legend>搜索课件</legend><br><br>
					<label><div align="right">课件标题：</div></label><input name="title" type="text" style="width:300px;" class="text" value="" /><br/><br/>
					<label><div align="right">课件状态：</div></label>
					<select name="state" style="width:300px;">
					    <option value="">请选择状态</option>
					    <option value="1">不显示</option>
					    <option value="0">显示</option>
					</select><br/><br/>
					<label><div align="right">下载次数：</div></label><input name="times" type="text" style="width:300px;" class="text" value=""><br/><br/>
<!--					<label><div align="right">排列序号：</div></label><input name="xuhao" type="text" style="width:300px;" class="text" value=""><br/><br/>-->
					<label><div align="right">入库时间：</div></label><input name="intotime" type="text" style="width:300px;" class="text" value=""><br/><br/>
					<label><div align="right">课件简介：</div></label><textarea name="introduction1" style="width:500px;height:200px;"></textarea><br /><br/>
					<div class="submit">
					<label></label><input type="submit" value="确认搜索"/>&nbsp;&nbsp;&nbsp;&nbsp;<a class="lj" href="teacher_courseware.php">返回列表</a></div>
			</fieldset>
			</form>
			</div>
			</td>
		</tr>
	</table>
<?php
}
function get_courseware_writer($i){
	global $db;
	$sql = "select * from nettteach_expert";
	$result = $db -> query($sql);
	while($row = $db -> fetch_array($result)){
		if($row['id'] == $i){
		    $str.="<option value='".$row['id']."' selected>".$row['name']."</option>";
		}
		else{
		    $str.="<option value='".$row['id']."'>".$row['name']."</option>";
		}
	}
	return $str;
}
?>

<?php
//删除课件
function delete_courseware()
{
    global $db;
	$i=0;
	$courseware_id = $_POST['id'];
	while($courseware_id){
	    if($courseware_id[$i]==null) break;
        $sql = "select * from nettteach_courseware where id = ".$courseware_id[$i]."";
        $result =$db -> query($sql);
        $row=$db ->fetch_array($result);
        $file=$row['address'];$file = substr($file,1,strlen($row['address'])); 
        if (unlink($file)) {
//           echo "The file was deleted successfully.", "n";
        } else {
//           echo "The specified file could not be deleted. Please try again.", "n";
        }
		$sql = "delete from nettteach_courseware where id = ".$courseware_id[$i]."";
	    $db -> query($sql);
	    $i++;
	}
	fs_showmsg('删除成功!');
}
?>
